package com.keymozy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keymozy.mapper.BorrowMapper;
import com.keymozy.mapper.MChildMapper;
import com.keymozy.mapper.ModelMapper;
import com.keymozy.mapper.StaffMapper;
import com.keymozy.pojo.Borrow;
import com.keymozy.pojo.MChild;
import com.keymozy.pojo.Model;
import com.keymozy.pojo.Staff;
import com.keymozy.service.BorrowService;
import com.keymozy.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class IBorrowService extends ServiceImpl<BorrowMapper, Borrow> implements BorrowService {
    @Autowired
    private MChildMapper mChildMapper;
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private ModelMapper modelMapper;
    @Override
    public String borrowModel(Integer id,Integer borrowTime,int cost) {
        Integer everydayMoney=0;
        Integer totalMoney=0;
        Staff staff=(Staff) TokenUtil.data.get();
        Integer userId = staff.getId();
        LocalDate endTime = LocalDate.now().plusDays(borrowTime);
        LocalDate beginTime = LocalDate.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String rtime = dateTimeFormatter.format(endTime);
        String btime = dateTimeFormatter.format(beginTime);
        if(cost>=0&&cost<100){
            everydayMoney=10;
            totalMoney=everydayMoney*borrowTime;
        }else if(cost>=100&&cost<500){
            everydayMoney=30;
            totalMoney=everydayMoney*borrowTime;
        }else if(cost>=500&&cost<1000){
            everydayMoney=50;
            totalMoney=everydayMoney*borrowTime;
        }else {
            everydayMoney=100;
            totalMoney=everydayMoney*borrowTime;
        }
        Borrow borrow = new Borrow();
        borrow.setMid(id);
        borrow.setSid(userId);
        borrow.setBtime(btime);
        borrow.setRtime(rtime);
        borrow.setCost(totalMoney);
        System.out.println(borrow+"总租借费用");
        boolean save = this.save(borrow);
        if(save){
            MChild mChild = mChildMapper.selectOne(new LambdaQueryWrapper<MChild>().eq(MChild::getMid, id));
            mChild.setState(2);
            int i = mChildMapper.updateById(mChild);
            if(i>0){
                return "租借成功";
            }
        }
        return "租借失败";
    }

    @Override
    public Map<String, Object> borrowList(Integer current, Integer size,Borrow borrow) {
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(borrow!=null){
            borrowLambdaQueryWrapper.eq(borrow.getId()!=null,Borrow::getId,borrow.getId());
            borrowLambdaQueryWrapper.eq(borrow.getMid()!=null,Borrow::getMid,borrow.getMid());
            borrowLambdaQueryWrapper.eq(borrow.getRtime()!=null,Borrow::getRtime,borrow.getRtime());
            borrowLambdaQueryWrapper.eq(borrow.getSid()!=null,Borrow::getSid,borrow.getSid());
            borrowLambdaQueryWrapper.eq(borrow.getBtime()!=null,Borrow::getBtime,borrow.getBtime());
        }
        Page<Borrow> borrowPage = baseMapper.selectPage(new Page<Borrow>(current, size), borrowLambdaQueryWrapper);
        long pageSize = borrowPage.getSize();
        List<Borrow> records = borrowPage.getRecords();
        for (Borrow record : records) {
            Integer sid = record.getSid();
            Integer mid = record.getMid();
            Staff staff = staffMapper.selectOne(new LambdaQueryWrapper<Staff>().eq(Staff::getId, sid).select(Staff::getUname));
            Model model = modelMapper.selectOne(new LambdaQueryWrapper<Model>().eq(Model::getId, mid).select(Model::getMname));
            record.setSname(staff.getUname());
            record.setMname(model.getMname());
        }
        long total = borrowPage.getTotal();
        long pageCurrent = borrowPage.getCurrent();
        int borrowSize = records.size();
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize",pageSize);
        map.put("records",records);
        map.put("total",total);
        map.put("current",pageCurrent);
        map.put("size",borrowSize);
        return map;
    }
}
